package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.dao.Queryprocessor;
import org.veridiandynamics.server.domain.Patient;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * This class generates interfaces that allow nurses to edit patient information 
 * in the database.
 * 
 * @author Lee
 */
public class EditPatientPanel extends AbstractPanelLayout
{
	private JButton back;
	private Container container;
	private CardLayout c1;
	private JTextField lastName;
	private JTextField firstName;
	private JTextField middleName;
	private JLabel lblMiddle;
	private JLabel lblFirstName;
	private JTextField addressField;
	private JTextField cityField;
	private JTextField zipField;
	private JLabel lblAddress;
	private JLabel lblCitystate;
	private JLabel lblZip;
	private JLabel lblTelephoneHome;
	private JLabel lblCell;
	private JLabel lblEmailAddress;
	private JTextField homeField;
	private JTextField cellField;
	private JTextField emailField;
	private JLabel lblMedicalInformation;
	private JLabel lblPharmacy;
	private JLabel lblInsuranceCarrier;
	private JTextField pharmField;
	private JTextField insuranceField;
	private JLabel lblAllergiesPlease;
	private JTextField allergiesField;
	private JButton submit;
	private JTextField idText;

	private Map<String, Object> editPatientMap;
	private JLabel lblEditUsername;
	private JTextField textField;
	private JPanel findPatient;
	private JButton btnSubmit;

	/**
	 * Constructor for the edit patient panels.
	 * 
	 * @param c1
	 * @param container
	 */
	public EditPatientPanel(final CardLayout c1, final Container container)
	{
		numCenterPanels = 1;
		this.c1 = c1;
		this.container = container;
		initialize();

		panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.Y_AXIS));
		findPatient = new JPanel();
		findPatient.setPreferredSize(new Dimension(0, 22));
		JLabel enterID = new JLabel("Please enter the Patient's Username");
		idText = new JTextField(10);
		findPatient.add(enterID);
		findPatient.add(idText);
		panelLeft.add(findPatient);
		MainWindow.welcomeMessage.setText("Edit Patient");

		JPanel subLeft = new JPanel();

		subLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent event) {
				String theUsername = idText.getText();
				HibernateDriver driver = new HibernateDriver();
				Patient patient2 = new Patient();
				long id = Queryprocessor.UsernametoID(theUsername);
				patient2 = (Patient) driver.RetrieveObject(id, patient2);
				lastName.setText(patient2.getLastName());
				firstName.setText(patient2.getFirstName());
				middleName.setText(patient2.getMiddleName());
				addressField.setText(patient2.getStreetAddress());
				cityField.setText(patient2.getCity());
				zipField.setText(patient2.getZip() + "");
				homeField.setText(patient2.getTelephone());
				cellField.setText(patient2.getCell());
				emailField.setText(patient2.getEmail());
				pharmField.setText(patient2.getPharmacy());
				insuranceField.setText(patient2.getInsuranceCarrier());
				allergiesField.setText(patient2.getAllergies());
				textField.setText(patient2.getUsername());		
			}
		});
		subLeft.add(btnSubmit, "8, 2");

		JLabel lblPersonalInformation = new JLabel("1.  Personal Information");
		subLeft.add(lblPersonalInformation, "4, 4");

		JLabel lblLastName = new JLabel("Last Name");
		subLeft.add(lblLastName, "4, 6");

		lblFirstName = new JLabel("First Name");
		subLeft.add(lblFirstName, "6, 6");

		lblMiddle = new JLabel("Middle");
		subLeft.add(lblMiddle, "8, 6");

		lastName = new JTextField();
		subLeft.add(lastName, "4, 8, fill, default");
		lastName.setColumns(10);

		firstName = new JTextField();
		subLeft.add(firstName, "6, 8, fill, default");
		firstName.setColumns(10);

		middleName = new JTextField();
		subLeft.add(middleName, "8, 8, fill, default");
		middleName.setColumns(10);

		lblAddress = new JLabel("Address");
		subLeft.add(lblAddress, "4, 10");

		lblCitystate = new JLabel("City/State");
		subLeft.add(lblCitystate, "6, 10");

		lblZip = new JLabel("Zip");
		subLeft.add(lblZip, "8, 10");

		addressField = new JTextField();
		subLeft.add(addressField, "4, 12, fill, default");
		addressField.setColumns(10);

		cityField = new JTextField();
		subLeft.add(cityField, "6, 12, fill, default");
		cityField.setColumns(10);

		zipField = new JTextField();
		subLeft.add(zipField, "8, 12, fill, default");
		zipField.setColumns(10);

		lblTelephoneHome = new JLabel("Telephone - Home");
		subLeft.add(lblTelephoneHome, "4, 14");

		lblCell = new JLabel("Cell");
		subLeft.add(lblCell, "6, 14");

		lblEmailAddress = new JLabel("Email Address");
		subLeft.add(lblEmailAddress, "8, 14");

		homeField = new JTextField();
		subLeft.add(homeField, "4, 16, fill, default");
		homeField.setColumns(10);

		cellField = new JTextField();
		subLeft.add(cellField, "6, 16, fill, default");
		cellField.setColumns(10);

		emailField = new JTextField();
		subLeft.add(emailField, "8, 16, fill, default");
		emailField.setColumns(10);

		lblMedicalInformation = new JLabel("2.  Medical Information");
		subLeft.add(lblMedicalInformation, "4, 18");

		lblPharmacy = new JLabel("Pharmacy");
		subLeft.add(lblPharmacy, "4, 20");

		lblInsuranceCarrier = new JLabel("Insurance Carrier");
		subLeft.add(lblInsuranceCarrier, "6, 20");

		pharmField = new JTextField();
		subLeft.add(pharmField, "4, 22, fill, default");
		pharmField.setColumns(10);

		insuranceField = new JTextField();
		subLeft.add(insuranceField, "6, 22, fill, default");
		insuranceField.setColumns(10);

		lblAllergiesPlease = new JLabel("Allergies - Please list");
		subLeft.add(lblAllergiesPlease, "4, 24");

		allergiesField = new JTextField();
		subLeft.add(allergiesField, "4, 26, 3, 1, fill, default");
		allergiesField.setColumns(10);

		back = new JButton("Back");
		back.addActionListener(new ActionListener() 
		{
			/**
			 * This method returns the user back to the nurse panel.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
				MainWindow.welcomeMessage.setText("Welcome");
			}
		});


		panelLeft.add(subLeft);

		lblEditUsername = new JLabel("2.  Edit username");
		subLeft.add(lblEditUsername, "4, 28, left, default");

		textField = new JTextField();
		subLeft.add(textField, "6, 28, fill, default");
		textField.setColumns(10);
		
		submit = new JButton("Edit Patient");
		submit.addActionListener(new ActionListener()
		{
			/**
			 * This method handles the editing of patients in the databse.
			 */
			public void actionPerformed(ActionEvent event)
			{
				String username = idText.getText();
				String last = lastName.getText();
				String first = firstName.getText();
				String middle = middleName.getText();
				String address = addressField.getText();
				String city = cityField.getText();
				String zip = zipField.getText();
				String homeTel = homeField.getText();
				String cell = cellField.getText();
				String email = emailField.getText();
				String pharmacy = pharmField.getText();
				String insurance = insuranceField.getText();
				String allergies = allergiesField.getText();
				String usernameUpdate = textField.getText();

				//				Map<String, Object> requestMap = new HashMap<String, Object>();
				//				requestMap.put("requestType", "editPatient");
				//				requestMap.put("patientUsername", username);
				//				requestMap.put("last", last);
				//				requestMap.put("first", first);
				//				requestMap.put("middle", middle);
				//				requestMap.put("address", address);
				//				requestMap.put("city", city);
				//				requestMap.put("telephone", homeTel);
				//				requestMap.put("cell", cell);
				//				requestMap.put("email", email);
				//				requestMap.put("pharmacy", pharmacy);
				//				requestMap.put("insurance", insurance);
				//				requestMap.put("allergies", allergies);
				//				requestMap.put("setUsername", usernameUpdate);

				HibernateDriver driver = new HibernateDriver();
				Patient patient2 = new Patient();
				long id = Queryprocessor.UsernametoID(username);
				patient2 = (Patient) driver.RetrieveObject(id, patient2);
				patient2.setLastName(last);
				patient2.setFirstName(first);
				patient2.setMiddleName(middle);
				patient2.setStreetAddress(address);
				patient2.setTelephone(homeTel);
				patient2.setCell(cell);
				patient2.setCity(city);
				patient2.setZip(Integer.parseInt(zip));
				patient2.setEmail(email);
				patient2.setPharmacy(pharmacy);
				patient2.setAllergies(allergies);
				patient2.setInsuranceCarrier(insurance);
				patient2.setUsername(usernameUpdate);
				driver.UpdateObject(patient2);

				try {
					Integer.parseInt(zip);
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "Please enter an integer for zip");
				}

				JOptionPane.showMessageDialog(null, "Patient edited");
			}
		});
		JPanel subLeft2 = new JPanel();
		subLeft2.add(submit, "4, 28, 2, 1");
		subLeft2.add(back);
		panelLeft.add(subLeft2);
	}

}
